home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-05-21 | 3.0 KB | 90 lines | [TEXT/ttxt] |
- -- <<<-
- module ValueTest1 uses ScriptX end
- in module ValueTest1
-
- class Payment (Pair)
- instance methods
- method add self key myValue -> (
- -- specialize the add method to do type checking
- if ((self.size = 0) and (not getClass myValue = Date)) do (
- format debug "first element not a date\n" undefined @normal
- )
- if ((self.size = 1) and (not isAKindOf myValue Number)) do (
- format debug "second element not a number\n" undefined @normal
- )
- apply nextMethod self key myValue
- )
- method presentValue self today discountRate -> (
- if not isAKindOf discountRate Number do (
- format debug "third arg not a number\n" undefined @normal
- )
- -- handles dates that have already been converted to large integer
- if (getClass today = Date) then (
- -- there are (60 * 60 * 24 * 365.25) seconds in a year
- local secondsPerYear := 31557600
- local y := (today as LargeInteger)/secondsPerYear
- local z := (self[1] as LargeInteger)/secondsPerYear
- -- return the present value of the payment
- return (self[2] * exp (negate(discountRate * (z - y))))
- ) else (
- format debug "second arg not a date\n" undefined @normal
- )
- )
- method prin self arg stream -> (
- prin "You will receive $" @unadorned debug
- prin self[2] @unadorned debug; prin " on " @unadorned debug
- prin (self[1] as String) @unadorned debug; prin "\n" @unadorned debug
- )
- end -- Payment
-
- class PaymentList (IndirectCollection)
- instance methods
- method init self #rest args -> (
- apply nextMethod self targetCollection:(new SortedArray) args
- )
- -- this method is called automatically before an object is added
- -- the object does not get added to the collection unless it returns true
- method isAppropriateObject self addedObject -> (
- -- check that it is the right kind of object
- if not isAKindOf addedObject Payment then (
- format debug "not a pair\n" undefined @normal
- return false
- ) else (
- return true
- )
- )
- -- this method is called automatically after an object is added
- method objectAdded self key obj -> (
- format debug "Your total is %*\n" (self.total) @normal
- )
- -- total is an example of a virtual instance variable
- method totalGetter self -> (
- local sum := 0
- forEach self (x -> sum := sum + x[2]) undefined
- return sum
- )
- -- generic prin takes care of all printing functions
- method prin self arg stream -> (
- forEach self (x->prin x @unadorned debug) undefined
- )
- method netPresentValue self discountRate -> (
- local sum := 0 -- initialization
- local today := theCalendarClock.date
- -- calls a function, defined inline, on each member of the collection
- forEach self (x -> sum := \
- sum + presentValue x today discountRate) undefined
- return sum
- )
- end -- PaymentList
-
- -- create an instance, and then add some data to it
- object myWinnings (PaymentList) end
- for i in 1 to 20 do (
- add myWinnings empty (new Payment \
- values:#(new Date year:(1995 + i) month:@january,1000000))
- )
- netPresentValue myWinnings 0.06
- netPresentValue myWinnings 0.10
-
- -->>>
-